<?php
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */
class Module
{
    public static $current = false;  
    public $data = false;
    public static $layout = 'default';
    function __construct()
    {                
        
        Module::$current = &$this;  
        Module::$current->data = self::get();
    }
    /**
     * Rum module 
     */
    public function run()
    {               
        
        // Load Module   
        System::registerFile('GSH_'. $this->data['module_name'], MODULE_PATH . DS);   
        $fncName = $this->data['module_name']; 
        $objFnc = new $fncName();     
        // Submit      
        if(Obj()->get('formModuleId', 'int', 0) != 0 && $this->data['moduleid'] == Obj()->get('formModuleId', 'int', 0))
        {               
            $objFnc->is_submit();           
        }
        // View
        if($this->data['type'] == 1 || Obj()->get('callback', 'str', '') == '')    
            echo $objFnc->draw(); 
        else
        {           
            $html = $objFnc->draw();  
            header("Content-type: text/plain");  
            //header('Content-Type: text/javascript; charset=utf8');
            echo Obj()->get('callback', 'str', '') .'({"html": '. json_encode($html) .'})';
        }
    }
    
    /**
     * 
     */
    protected static function get()
    {
        if(Obj()->get('fnc') != '' && Obj()->get('fnc') != 'game_display' && Obj()->get('fnc') != 'login' && Obj()->get('fnc') != 'register' && Obj()->get('gameid', 'int', 0) != 0 && substr(Obj()->get('fnc'), 0, 5) != ADMIN_PATH)
        {
            $sqlGame = ' AND module.gameid = '. Obj()->get('gameid', 'int', 0);    
        }
        else
            $sqlGame = '';    
            
        db()->query('SELECT module.moduleid,module.gameid,module.name as module_name,module.status as module_status, page.pageid,page.name,page.status,page.type
                    FROM modules as module
                    LEFT JOIN pages as page ON(module.pageid = page.pageid)
                    WHERE page.name = "'. Obj()->get('fnc') .'" AND page.status = 1 AND type = '. (substr(Obj()->get('fnc'), 0, 5) == ADMIN_PATH ? '1' : '0') . $sqlGame .'
                    LIMIT 0,1');
        $module = db()->fetch();  
        if(!$module)
            exit();
        return $module;
    }
}

// Initialize Module
Module::$current = new Module();
?>
